// Package service @program: account
//@author: edte
//@create: 2021-12-28 13:58
package service

import (
	useraccount "account/config"
	"account/database"
	log2 "account/log"
	"account/tools"
	"context"
	"fmt"
	"net/http"
	"strconv"
)

type AdminPower struct {
	useraccount.UnimplementedAdminServerServer
}

func (ap AdminPower) AdminLogin(_ context.Context, in *useraccount.LoginAdminFormRequest) (response *useraccount.LoginUserFormResponse, err error) {
	response = &useraccount.LoginUserFormResponse{}
	response.UserInfo = &useraccount.QueryUserFormResponse{}
	user, err := database.AdminLogin(in.AdminPhone, in.AdminPassword)
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminLogin", in.AdminPhone, "Error", "Account login failure because "+err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		/*		response.UserInfo.UserId = int32(user.Id)
				response.UserInfo.UserPhone = user.AdminNum
				response.UserInfo.UserPassword = user.AdminPD*/
		return response, err
	}
	response.Status = http.StatusOK
	response.UserInfo.UserId = int32(user.Id)
	response.UserInfo.UserPhone = user.AdminNum
	response.UserInfo.UserPassword = user.AdminPD

	writeLog, err := log2.WriteLog(context.Background(), "account", "AdminLogin", strconv.Itoa(user.Id), "Debug", "Login succeeded.")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err != nil {
		fmt.Println("写入日志失败")
	}

	return response, err
}
func (ap AdminPower) AdminRegister(_ context.Context, in *useraccount.RegisterAdminFormRequest) (response *useraccount.RegisterAdminFormResponse, err error) {
	user := tools.AdminUser{AdminNum: in.AdminPhone, AdminPD: in.AdminPassword, PrivilegeLevel: int(in.AdminLever)}
	response = &useraccount.RegisterAdminFormResponse{}
	err = database.AdminRegister(user)
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminRegister", in.AdminPhone, "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminRegister", in.AdminPhone, "Debug", "The administrator is registered successfully.")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err
}
func (ap AdminPower) AdminQueryData(_ context.Context, in *useraccount.QueryUserInfoRequest) (response *useraccount.QueryUserInfoResponse, err error) {
	users, err := database.AdminQueryData(int(in.StartUserId), int(in.EndUserId))
	response = &useraccount.QueryUserInfoResponse{}
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminQueryData", "("+strconv.Itoa(int(in.StartUserId))+","+strconv.Itoa(int(in.EndUserId))+")", "Warring", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.UserForms = nil
		response.Status = http.StatusBadRequest
		return response, err
	}

	for _, user := range users {
		user2 := useraccount.QueryUserFormResponse{}
		user2.UserId = int32(user.Id)
		user2.UserPhone = user.UserPhone
		user2.UserPassword = user.Password
		user2.Email = user.EmailNumber
		user2.Nickname = user.NickName
		response.UserForms = append(response.UserForms, &user2)
		//users2= append(users2, string(marshal)
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminQueryData", "("+strconv.Itoa(int(in.StartUserId))+","+strconv.Itoa(int(in.EndUserId))+")", "Warring", "The administrator succeeds in querying user information")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err

}
func (ap AdminPower) AdminBlockUser(_ context.Context, in *useraccount.BlockUserRequest) (response *useraccount.BlockUserResponse, err error) {
	response = &useraccount.BlockUserResponse{}
	err = database.AdminBlockDb(int(in.UserId))
	if err != nil {

		writeLog, err2 := log2.WriteLog(context.Background(), "account", "AdminBlockUser", strconv.Itoa(int(in.UserId)), "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		response.Status = http.StatusBadRequest
		return response, err
	}
	writeLog, err := log2.WriteLog(context.Background(), "account", "AdminBlockUser", strconv.Itoa(int(in.UserId)), "Error", "The user is blocked successfully. Procedure")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err != nil {
		fmt.Println("写入日志失败")
	}
	response.Status = http.StatusOK
	return response, err
}
